use v5.14;

#==============================================================================
#
# Name: lsusb.pl
#
# Written by: Samuel Barton
#
# Date: February - March 2014
#
# Project: Log-Based sensors
#
# Description: This file will execute the lsusb command, catch its output, and
#              put relevant data in text files for each type of device. The
#              data from lsusb will be filtered by filter files for each type
#              of device.
#
#==============================================================================

# list of currently connected USB devices (populated by lsusb command)
my @lsusb = `lsusb`;

# paths of files to put device ids in
my $output_instant_exec = "INSTANT_EXEC.dev";
my $output_listen_log = "LISTEN_LOG.dev";

# paths of file used as filters for the data in @lsusb
my $instant_exec_filter = "INSTANT_EXEC.filter";
my $listen_log_filter = "LISTEN_LOG.filter";

# open the file for output (overwrite everything in the file already)
open(instant_exec, ">", 
     $output_instant_exec) or die "Couldn't open file for output: $!";
open(listen_log, ">",
     $output_listen_log) or die "Couldn't open file for output: $!";

# loop through the list of connected devices and print out the device ids to 
# the connected devices file.
for my $line (@lsusb)
{
    # Get the manufacturer and device id
    (my $manufacturer_id, my $product_id) = 
    ($line =~ /([a-zA-Z0-9]{4}):([a-zA-Z0-9]{4})/);

    # if the dev_id is in the list of supported devices, put it in the output
    # file, otherwise ignore it.
    my $dev_id = $manufacturer_id.'_'.$product_id;

    if (&isInstantExec($dev_id))
    {
        print instant_exec $dev_id."\n";
    }
    elsif (&isListenLog($dev_id))
    {
        print listen_log $dev_id."\n";
    }
}

close(instant_exec);
close(listen_log);

#==============================================================================
#
# Method name: isInstantExec
#
# Parameters: $dev_id - the manufacturer and product id of the device
#
# Returns: 1 if device in the list of connected devices, 0 otherwise
#
# Description: This method determines if the device referenced by this id is 
#              on the list of devices with log scripts which execute instantly.
#
#              The method loops through the filter list of devices, and if
#              the device referenced by $dev_id is on the list, the method 
#              closes the filter file and returns 1. If $dev_id is not in the 
#              list of supported devices the method returns 0.
#
#==============================================================================
sub isInstantExec
{
    my $dev_id = $_[0];

    open(filter, "<", 
         $instant_exec_filter) or die "Couldn't open file to read: $!";

    for my $dev (<filter>)
    {
        chomp($dev);

        if ($dev_id eq $dev)
        {
            close(filter);
            return 1;
        }
    }

    close(filter);
    return 0;
}


#==============================================================================
#
# Method name: isListenLog
#
# Parameters: $dev_id - the manufacturer and product id of the device
#
# Returns: 1 if device in the list of connected devices, 0 otherwise
#
# Description: This method determines if the device referenced by this id is 
#              on the list of devices which listen for data and log it when it
#              appears.
#
#              The method loops through the filter list of devices, and if
#              the device referenced by $dev_id is on the list, the method 
#              closes the filter file and returns 1. If $dev_id is not in the 
#              list of supported devices the method returns 0.
#
#==============================================================================
sub isListenLog
{
    my $dev_id = $_[0];

    open(filter, "<", 
         $listen_log_filter) or die "Couldn't open file to read: $!";

    for my $dev (<filter>)
    {
        chomp($dev);

        if ($dev_id eq $dev)
        {
            close(filter);
            return 1;
        }
    }

    close(filter);
    return 0;
}
